﻿Imports System.Data.OleDb

Public Class UserModel
    Inherits Model

    Private _fullName As String
    Private _description As String
    Private _picture As String

    Private Shared ReadOnly Property DefaultPicture() As String
        Get
            Return "~/assets/blank-face.gif"
        End Get
    End Property

    Public ReadOnly Property UserName() As String
        Get
            Return ID
        End Get
    End Property

    Public Property FullName() As String
        Get
            Return If(HasFullName, _fullName, UserName)
        End Get
        Set(ByVal value As String)
            If (_fullName <> value) Then
                _fullName = value
                _isSaved = False
            End If
        End Set
    End Property

    Public Property Description() As String
        Get
            Return If(HasDescription, _description, UserName & " has not written a description yet.")
        End Get
        Set(ByVal value As String)
            If (_description <> value) Then
                _description = value
                _isSaved = False
            End If
        End Set
    End Property

    Public Property Picture() As String
        Get
            Return If(HasPicture, _picture, DefaultPicture)
        End Get
        Set(ByVal value As String)
            If (_picture <> value) Then
                _picture = value
                _isSaved = False
            End If
        End Set
    End Property

    Public ReadOnly Property HasFullName As Boolean
        Get
            Return _fullName <> ""
        End Get
    End Property

    Public ReadOnly Property HasDescription As Boolean
        Get
            Return _description <> ""
        End Get
    End Property

    Public ReadOnly Property HasPicture() As Boolean
        Get
            Return _picture <> ""
        End Get
    End Property

    Public ReadOnly Property IsEmpty As Boolean
        Get
            Return Not (HasFullName OrElse HasDescription OrElse HasPicture)
        End Get
    End Property

    Public Sub New(ByVal newId As String)
        MyBase.New(newId)
    End Sub

    Protected Overrides Sub CreateBlank()
        _fullName = ""
        _description = ""
        _picture = ""
    End Sub

    Protected Overrides Function Load() As Boolean
        Dim foundInDB As Boolean
        Dim sqlString As String = "SELECT FullName, Description, Picture FROM profiles WHERE UserName=@UserName"
        Dim oleDbConn As OleDbConnection = NeonLearn.db()
        Dim cmd As OleDbCommand = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@UserName", UserName)
        Dim dataReader As OleDbDataReader = cmd.ExecuteReader()
        foundInDB = dataReader.HasRows
        If foundInDB Then
            dataReader.Read()
            GetField(_fullName, dataReader, "FullName", "")
            GetField(_description, dataReader, "Description", "")
            GetField(_picture, dataReader, "Picture", "")
        End If
        oleDbConn.Close()
        Return foundInDB
    End Function

    Public Overrides Function Save() As Boolean
        If IsSaved Then
            Return True
        End If
        Dim sqlString As String
        Dim cmd As OleDbCommand
        Dim rowsAffected As Integer
        Dim oleDbConn As OleDbConnection = NeonLearn.db()
        If IsInDB Then
            sqlString = "UPDATE profiles SET FullName=@FullName, Description=@Description, Picture=@Picture WHERE UserName=@UserName"
        Else
            sqlString = "INSERT INTO profiles (FullName, Description, Picture, UserName) VALUES (@FullName, @Description, @Picture, @UserName)"
        End If
        cmd = New OleDbCommand(sqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@FullName", If(HasFullName, FullName, DBNull.Value))
        cmd.Parameters.AddWithValue("@Description", If(HasDescription, Description, DBNull.Value))
        cmd.Parameters.AddWithValue("@Picture", If(HasPicture, Picture, DBNull.Value))
        cmd.Parameters.AddWithValue("@UserName", UserName)
        rowsAffected = cmd.ExecuteNonQuery()
        oleDbConn.Close()
        If rowsAffected = 1 Then
            IsInDB = True
            _isSaved = True
            Return True
        Else
            Return False
        End If
    End Function
End Class

